/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-25 21:12:01
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.bp.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.bean.BpIndustrialInjureApplyDetailBean;
import com.joysuch.wwyt.bp.bean.BpIndustrialInjureApplyEditBean;
import com.joysuch.wwyt.bp.bean.BpIndustrialInjureApplyListBean;
import com.joysuch.wwyt.bp.bean.BpIndustrialInjureApplyListBean.SearchBpIndustrialInjureApply;
import com.joysuch.wwyt.bp.entity.BpIndustrialInjureApply;
import com.joysuch.wwyt.bp.entity.dsl.QBpIndustrialInjureApply;
import com.joysuch.wwyt.bp.repository.BpAccidentDao;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.bp.repository.BpIndustrialInjureApplyDao;
import com.joysuch.wwyt.bp.service.BpIndustrialInjureApplyService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BaseAttachmentRepositoryService;
import com.joysuch.wwyt.core.service.BaseUserService;
import com.joysuch.wwyt.mh.BizUtils;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class BpIndustrialInjureApplyServiceImpl implements BpIndustrialInjureApplyService {

    @Autowired
    private BpIndustrialInjureApplyDao dao;
    @Autowired
    private BpAccidentDao bpAccidentDao;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpDepartDao bpDepartDao;
    @Autowired
    private BaseTypeGroupItemDao baseTypeGroupItemDao;

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private BaseAttachmentRepositoryService baseAttachmentRepositoryService;

    @Override
    public BpIndustrialInjureApply add(BpIndustrialInjureApplyEditBean entity) {
        BpIndustrialInjureApply content = new BpIndustrialInjureApply();
        if (entity.getAttachment() != null) {
            content.setAttachmentId(entity.getAttachment().getId());
        }
        BeanUtils.copyProperties(entity, content, "id");
        content.setApplyUserName(baseUserDao.getRealNameById(entity.getApplyUserId()));
        content.setApplyUserDepartName(bpDepartDao.findDepartNameById(entity.getApplyUserDepartId()));
        dao.save(content);
        return content;
    }

    @Override
    public void update(BpIndustrialInjureApplyEditBean entity) {
        BpIndustrialInjureApply content = dao.findById(entity.getId()).get();
        if (entity.getAttachment() != null) {
            content.setAttachmentId(entity.getAttachment().getId());
        }
        BeanUtils.copyProperties(entity, content, "id");
        content.setApplyUserName(baseUserDao.getRealNameById(entity.getApplyUserId()));
        content.setApplyUserDepartName(bpDepartDao.findDepartNameById(entity.getApplyUserDepartId()));
        dao.save(content);
    }

    @Override
    public Page<BpIndustrialInjureApplyListBean> findByPage(Pageable page, SearchBpIndustrialInjureApply condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<BpIndustrialInjureApply> data = predicate == null ? dao.findAll(page) : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data,
                new PageDataConvert<BpIndustrialInjureApplyListBean, BpIndustrialInjureApply>() {

                    @Override
                    public BpIndustrialInjureApplyListBean convert(BpIndustrialInjureApply f) {
                        BpIndustrialInjureApplyListBean bean = new BpIndustrialInjureApplyListBean();
                        BeanUtils.copyProperties(f, bean);
                        bpAccidentDao.findById(f.getAccidentId()).ifPresent(accident -> {
                            bean.setAccidentCode(accident.getCode());
                            bean.setAccidentName(accident.getName());
                            bean.setAccidentHappenTime(accident.getHappenTime());
                        });
                        return bean;
                    }

                });
    }

    private Predicate prepareListSearchCondition(SearchBpIndustrialInjureApply condition) {
        Predicate p = null;
        QBpIndustrialInjureApply q = QBpIndustrialInjureApply.bpIndustrialInjureApply;
        if (condition.getAccidentId() != null) {
            p = ExpressionUtils.and(p, q.accidentId.eq(condition.getAccidentId()));
        }
        if (condition.getCodeOrName() != null) {
            List<Long> accidentIds = bpAccidentDao.getIdByCodeContains(condition.getCodeOrName());
            Predicate p2 = ExpressionUtils.or(q.applyUserName.contains(condition.getCodeOrName()),
                    q.accidentId.in(accidentIds));
            p = ExpressionUtils.and(p, p2);
        }
        if (condition.getAccidentDepartId() != null) {
            List<Long> accidentIds = bpAccidentDao.getIdsByDepartId(condition.getAccidentDepartId());
            p = ExpressionUtils.and(p, q.accidentId.in(accidentIds));
        }
        return p;
    }

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     *
     * @param id
     *            要删除的业务ID
     * @throws BussinessRestrictionsException
     *             抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {
    }

    @Override
    public BpIndustrialInjureApply findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public BpIndustrialInjureApplyDetailBean getById(Long id) {
        BpIndustrialInjureApply entity = findById(id);
        if (entity == null) {
            return null;
        }
        BpIndustrialInjureApplyDetailBean bean = new BpIndustrialInjureApplyDetailBean();
        BeanUtils.copyProperties(entity, bean);
        bpAccidentDao.findById(entity.getAccidentId()).ifPresent(accident -> {
            bean.setAccidentCode(accident.getCode());
            bean.setAccidentName(accident.getName());
            bean.setAccidentHappenTime(accident.getHappenTime());
        });
        if (entity.getAttachmentId() != null) {
            bean.setAttachment(baseAttachmentRepositoryService.findByIdIntoBean(entity.getAttachmentId()));
        }
        bean.setInjuryTypeName(baseTypeGroupItemDao.getNameById(entity.getInjuryType()));
        bean.setAllMaterialOkName(BizUtils.translateYesNoToChinese(entity.getAllMaterialOk()));
        return bean;
    }

}
